import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { Observable, throwError } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ToastService {

  constructor(
    private toast: NzNotificationService,
    private translate: TranslateService,
  ) { }

  duration = 5000;
  duration_err = 0;

  success(title: string, content = '', params?: any) {
    title = this.translate.instant(title, params);
    this.toast.success(title, content, { nzDuration: this.duration });
  }

  info(title: string, content = '', params?: any) {
    title = this.translate.instant(title, params);
    this.toast.info(title, content, { nzDuration: this.duration });
  }

  warning(title: string, content = '', params?: any) {
    title = this.translate.instant(title, params);
    this.toast.warning(title, content, { nzDuration: this.duration });
  }

  error(title: string, content = '', params?: any) {
    title = this.translate.instant(title, params);
    this.toast.error(title, content, { nzDuration: this.duration_err });
  }

  blank(title: string, content = '', params?: any) {
    title = this.translate.instant(title, params);
    this.toast.blank(title, content, { nzDuration: this.duration });
  }

  handleError(error: any, title: string, content = '', param?: any): Observable<any> {
    this.toast.error(title, content, param);
    return throwError(error);
  }
}
